package com.mac.aircraft
{
   	import flash.display.Loader;
   	import flash.display.Sprite;
   	import flash.events.Event;
   	import flash.net.URLRequest;
   	
	    
    public class Aircraft extends Sprite
    {

		public var ldr:Loader;        
        public var xMove:Number;
        public var yMove:Number;
        public var xPrev:Number;
        public var yPrev:Number;
        
        public static var xCollisionDetect:Array;
        public static var yCollisionDetect:Array; 
        //public var stg:DisplayObjectContainer;

 		       
        public function Aircraft( maxW:int, maxH:int){
            
            //Setup arrays or arrays to keep track of position
            xCollisionDetect = new Array(maxW);
            yCollisionDetect = new Array(maxH);
			for (var i:int=0; i<maxW; i++){
				xCollisionDetect[i] = new Array();
			}

			for (var j:int=0; j<maxW; j++){
				yCollisionDetect[j] = new Array();
			}
			
			// load a sample aircraft image and add it to this sprite
			var req:URLRequest = new URLRequest( "../assests/ac.jpg" );
			ldr = new Loader();
			ldr.load( req );
			addChild( ldr );    

			setStartCoords( maxW, maxH );
            
            //X and Y end up making being random, so the angle and speed in which the "plane" travels is random as well            
            xMove = Math.random() * 10;
            yMove = Math.random() * 10;
            
            trace(this.name);
            
            // start step triggering function based on enterframe event          
            this.addEventListener(Event.ENTER_FRAME , fly);   
			//stg = stage;
			        
        }
        
/********************************************************************************************************************************/   
// randomize the start location
/********************************************************************************************************************************/ 
        public function setStartCoords( maxW:int, maxH:int ):* {
            this.x = Math.random() * maxW;        
            this.y = Math.random() * maxH;
            
            if ( Math.random() > 0.5 ) {
	            Math.random() < 0.5 ? y = 0 : y = maxH;            	
	        } else {
	        	Math.random() < 0.5 ? x = 0 : x = maxW;
	        }
	        
	        if (detectCollision())
	        	setStartCoords(maxW, maxH);
	        //set xCollisionDetect and yCollisionDetect, make sure there is no object in the start position
	        
        }

/********************************************************************************************************************************/ 
//Set coordinates aircraft should move to
/********************************************************************************************************************************/ 
        public function fly ( event:Event ):void{
            
            var collisionChkLst:Array = new Array();
            
            // To make "plane" bounce at edge         
            if ( this.x + xMove > stage.stageWidth ) 
            	xMove *= -1;
            else if ( this.x + xMove < 0 ) 
            	xMove *= -1;
            
            if ( this.y + yMove > stage.stageHeight ) 
            	yMove *= -1;
            else if ( this.y + yMove < 0 ) 
            	yMove *= -1;
            
            xPrev = this.x;
            yPrev = this.y;

			// set position           
            this.x += xMove;
            this.y += yMove;
            
            if (detectCollision())
            	trace("hit!!");
  
        }
/********************************************************************************************************************************/ 
//Detect if there are any collisions
/********************************************************************************************************************************/         
        public function detectCollision():Boolean{
           var collisionChkLst:Array = new Array();        	
 
             //Mark spot occupied by object in tracking array for X axis
            xCollisionDetect[this.x] = this.name;
            for (var i:int = 0; i<(this.width/2); i++){
				//check to see if any other object is occupying the same space(points to the right of the centre). 
				//If yes, then put in collisionChkLst (no duplicates tho)
				for (var j:int = 0; j<xCollisionDetect[this.x + (i)].length; j++){
					if (collisionChkLst.indexOf(xCollisionDetect[this.x + (i)][j]) == -1){
						collisionChkLst.push(xCollisionDetect[this.x + (i)])
					}
				}
				
				//check to see if any other object is occupying the same space(points to the left of the centre). 
				//If yes, then put in collisionChkLst (no duplicates tho)
				for (var k:int = 0; k<xCollisionDetect[this.x + (-1*i)].length; k++){
					if (collisionChkLst.indexOf(xCollisionDetect[this.x + (-1*i)][k]) == -1){
						collisionChkLst.push(xCollisionDetect[this.x + (-1*i)])
					}
				}
				
				xCollisionDetect[this.x + (i)].push(this.name);
				xCollisionDetect[this.x + (-1*i)].push(this.name);				

            }


            //Mark spot occupied by object in tracking array for Y axis
            xCollisionDetect[this.x] = this.name;
            for (var l:int = 0; l<(this.width/2); l++){
				//check to see if any other object is occupying the same space(points to the right of the centre). 
				//If yes, and the same object is in the collisionChkLst, then theres an intersetion on both the x and y coordinates
				//Thus return true
				for (var m:int = 0; m<yCollisionDetect[this.x + (l)].length; m++){
					if (collisionChkLst.indexOf(yCollisionDetect[this.x + (l)][m]) != -1){
						return true;
					}
				}
				
				//check to see if any other object is occupying the same space(points to the left of the centre). 
				//If yes, and the same object is in the collisionChkLst, then theres an intersetion on both the x and y coordinates
				//Thus return true
				for (var n:int = 0; n<yCollisionDetect[this.x + (-1*l)].length; n++){
					if (collisionChkLst.indexOf(xCollisionDetect[this.x + (-1*l)][n]) != -1){
						return true;
					}
				}
				
				yCollisionDetect[this.x + (i)].push(this.name);
				yCollisionDetect[this.x + (-1*i)].push(this.name);				

            } 			
  
			return false;
        }
        
    }
    
}